package com.carpedil.es.controller;

import com.carpedil.es.entity.SourceCodeEntity;
import com.carpedil.es.mapper.SourceCodeMapper;
import com.carpedil.es.service.UrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/** @author Administrator */
@Controller
@RequestMapping("/es")
public class ElasticSearchController {
  @Autowired UrlService urlService;
  @Autowired SourceCodeMapper sourceCodeMapper;

  @GetMapping("/save")
  @ResponseBody
  public String saveUrl(String url) {
    System.out.println("url = " + url);
    return urlService.save(url);
  }

  @GetMapping("/find/{keyword}")
  @ResponseBody
  public List<SourceCodeEntity> findSourceCodeByKeyword(@PathVariable("keyword") String keyword) {
    return sourceCodeMapper.findBySourceCodes(keyword);
  }

  @GetMapping({"/", "/index"})
  public String toIndex() {
    return "index";
  }
}
